@echo off
rem 设置中文编码，解决乱码问题
chcp 936 > nul
title Hello Frontend Tool
setlocal enabledelayedexpansion

rem =================================================================
rem FEATURES:
rem 1. One-click to start development server
rem 2. Multiple browser opening methods (for maximum compatibility)
rem 3. Compatible with all Node.js versions
rem 4. Multiple fallbacks if one method fails
rem =================================================================

rem 设置调试模式 (1=开启详细输出, 0=关闭)
set DEBUG_MODE=1

if "%DEBUG_MODE%"=="1" (
  echo [DEBUG] 开启调试模式...
  echo [DEBUG] 所有命令将被显示
  @echo on
)

echo ====================================
echo    Hello Frontend Startup Tool
echo ====================================
echo.

rem 记录Node版本信息，便于调试
echo [INFO] 检查Node.js环境...
node --version
if %ERRORLEVEL% NEQ 0 (
  echo [ERROR] Node.js未安装或不在PATH中
  echo [ERROR] 请安装Node.js并确保添加到系统PATH变量中
  goto :error_exit
)

rem Change to script directory
cd /d %~dp0
if %ERRORLEVEL% NEQ 0 (
  echo [ERROR] 切换到脚本目录失败: %~dp0
  goto :error_exit
)

echo [INFO] 当前工作目录: %cd%

rem Check dependencies
if not exist node_modules (
  echo [INFO] Dependencies not installed. Installing now...
  echo [INFO] 这可能需要几分钟时间，请耐心等待...
  
  call npm install --registry=https://registry.npmmirror.com
  if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] 依赖安装失败。
    echo [INFO] 尝试使用国内镜像源安装...
    call npm install --registry=https://registry.npm.taobao.org
    if %ERRORLEVEL% NEQ 0 (
      echo [ERROR] 依赖安装再次失败。
      goto :error_exit
    )
  )
  echo [SUCCESS] Dependencies installed.
  echo.
)

rem Create a temporary HTML file that will auto-redirect to our app
echo [INFO] 创建临时重定向页面...
echo ^<!DOCTYPE html^> > redirect.html
echo ^<html^> >> redirect.html
echo ^<head^> >> redirect.html
echo ^<meta http-equiv="refresh" content="0;URL='http://localhost:80/'" /^> >> redirect.html
echo ^<title^>Redirecting...^</title^> >> redirect.html
echo ^</head^> >> redirect.html
echo ^<body^> >> redirect.html
echo ^<p^>If you are not redirected automatically, click ^<a href="http://localhost:80/"^>here^</a^>.^</p^> >> redirect.html
echo ^<p^>If that doesn't work, try ^<a href="http://localhost:81/"^>this link instead^</a^>.^</p^> >> redirect.html
echo ^</body^> >> redirect.html
echo ^</html^> >> redirect.html

echo [INFO] Starting development server...
echo [INFO] A browser window will open in a moment...
echo [INFO] Press Ctrl+C to stop the server when done
echo.

rem Method 1: Open the redirect page directly in a new window
start redirect.html

echo [INFO] Browser windows should open automatically
echo [INFO] If browser doesn't open or shows blank page, manually visit:
echo [INFO] http://localhost:80/ or http://localhost:81/
echo.

rem 检查Vue CLI是否安装
echo [INFO] 检查Vue CLI...
call npx vue-cli-service --version > nul 2>&1
if %ERRORLEVEL% NEQ 0 (
  echo [WARNING] Vue CLI可能未正确安装
  echo [INFO] 尝试全局安装@vue/cli-service...
  call npm install -g @vue/cli-service
  if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] 无法安装Vue CLI
    goto :error_exit
  )
)

rem Set environment variable for Node.js and start server in foreground
echo [INFO] 设置Node环境变量...
set NODE_OPTIONS=--openssl-legacy-provider
rem 设置为开发环境，使用localhost API
set API_ENV=development

echo [INFO] 启动Vue开发服务器...
call npx vue-cli-service serve
if %ERRORLEVEL% NEQ 0 (
  echo.
  echo [ERROR] 服务器启动失败，错误代码: %ERRORLEVEL%
  echo [INFO] 尝试使用备用命令启动...
  call npm run dev
  if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] 备用启动命令也失败了
    goto :error_exit
  )
)

rem Check if the process ended with an error
if %ERRORLEVEL% NEQ 0 (
  echo.
  echo [ERROR] Server stopped unexpectedly with error code %ERRORLEVEL%
  echo [INFO] Please check the error messages above.
)

rem Clean up
if exist redirect.html del redirect.html

echo.
echo [INFO] Server has been stopped.
goto :normal_exit

:error_exit
echo.
echo [ERROR] 脚本执行过程中出现错误
echo [INFO] 请查看以上错误信息进行排查
echo [INFO] 常见问题:
echo [INFO] 1. Node.js版本过高或过低 (推荐使用Node.js 14-16)
echo [INFO] 2. 依赖项安装失败
echo [INFO] 3. 端口80或81被占用
echo.

:normal_exit
echo Press any key to exit...
pause > nul
exit /b %ERRORLEVEL% 